#include <QDebug>
#include <QMessageBox>

#include "unititems.h"
#include "ui_unititems.h"

#include "other/rosterelements.h"

UnitItems::UnitItems(const DualCollection<GameItem> *items, const DualCollection<GameCategory> *categories, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::UnitItems),
    factoryModel(items, categories),
    unitItemModel(items, categories),
    currentUnit(NULL), _items(items)
{
    Q_ASSERT(items && categories);
    ui->setupUi(this);
    ui->itemFactoryView->setModel(&factoryModel);
    ui->unitItemView->setModel(&unitItemModel);
    connect(ui->itemFactoryView->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
            this, SLOT(itemFactoryView_selectionChanged(QModelIndex,QModelIndex)));
    connect(ui->unitItemView->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
            this, SLOT(unitItemView_selectionChanged(QModelIndex,QModelIndex)));
}

UnitItems::~UnitItems()
{
    delete ui;
}

void UnitItems::setSourceUnit(RosterUnit *unit) {
    currentUnit = unit;
    factoryModel.setSource(unit);
    unitItemModel.setSource(unit);
    ui->itemFactoryView->expandAll();
    ui->unitItemView->expandAll();
}

void UnitItems::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void UnitItems::armyFileChanged() {
    // \TODO: It is possible to optimize this by using only one index!
    factoryModel.buildCategoryIndex();
    unitItemModel.buildCategoryIndex();
}

void UnitItems::itemFactoryView_selectionChanged(const QModelIndex& current, const QModelIndex& previous) {
    Q_UNUSED(previous)    
    if(!current.parent().isValid()) {
        ui->itemNameLabel->setText(tr("No item selected"));
        ui->descriptionLabel->setText("");
    } else {
        QString itemKey = factoryModel.getItemKeyFromIndex(current);
        GameItem *item = _items->getRealElement(itemKey);
        if(item) {
            ui->itemNameLabel->setText(item->getName());
            // \TODO: Item description not implemented
        }
    }
}

void UnitItems::unitItemView_selectionChanged(const QModelIndex& current, const QModelIndex& previous) {
    Q_UNUSED(previous)
    if(!current.parent().isValid()) {
        ui->buttonFrame->setEnabled(false);
    } else {
        ui->buttonFrame->setEnabled(true);
    }
}

void UnitItems::on_itemFactoryView_doubleClicked(const QModelIndex &index)
{
    if(currentUnit == NULL)
        return;
    QString key = factoryModel.getItemKeyFromIndex(index);
    if(!key.isEmpty()) {
        if(currentUnit->addItem(key, 1))
            unitItemModel.updateDataForKey(key);
        else
            QMessageBox::critical(0, tr("Cannot add item"), tr("The item was not added because it exceeded the limits"));
    }
}
void UnitItems::on_amountButton_clicked()
{
    if(currentUnit == NULL)
        return;
    QString currentKey = unitItemModel.getKeyFromIndex(ui->unitItemView->currentIndex());
    if(currentUnit->addItem(currentKey))
        unitItemModel.updateDataForKey(currentKey);
    else
        QMessageBox::critical(0, tr("Cannot amount item"), tr("The item was not added because it exceeded the limits"));
}

void UnitItems::on_subtractButton_clicked()
{
    if(currentUnit == NULL)
        return;
    QString currentKey = unitItemModel.getKeyFromIndex(ui->unitItemView->currentIndex());
    if(currentUnit->subtractItem(currentKey))
        unitItemModel.updateDataForKey(currentKey);
}

void UnitItems::on_deleteButton_clicked()
{
    if(currentUnit == NULL)
        return;
    QString currentKey = unitItemModel.getKeyFromIndex(ui->unitItemView->currentIndex());
    currentUnit->removeItem(currentKey);
    unitItemModel.updateDataForKey(currentKey);
}
